package com.qiantu.phone.widget.setting;

import static android.util.TypedValue.COMPLEX_UNIT_PX;

import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hjq.base.DensityUtils;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceBaseConfigInfo;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.api.utils.DeviceInfoParseUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.bean.WheelValueBean;
import com.qiantu.phone.event.DeviceItemChangeEvent;
import com.qiantu.phone.event.DeviceDetailChangeEvent;
import com.qiantu.phone.ui.dialog.BottomWheelViewDialog;
import com.qiantu.phone.ui.uiutil.DeviceType;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;
import com.lxj.xpopup.XPopup;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class DeviceSettingKeyChange extends SettingBar implements View.OnClickListener {
    private DeviceBean mDevice;
    private AppActivity mContext;
    public DeviceSettingKeyChange(AppActivity context, DeviceBean deviceBean, String title) {
        this(context, null, deviceBean, title);
    }

    public DeviceSettingKeyChange(AppActivity context, AttributeSet attrs, DeviceBean deviceBean, String title) {
        this(context, attrs, 0, deviceBean, title);
    }

    public DeviceSettingKeyChange(AppActivity context, AttributeSet attrs, int defStyleAttr, DeviceBean deviceBean, String title) {
        this(context, attrs, defStyleAttr, R.style.setting_item_style, deviceBean, title);
    }

    public DeviceSettingKeyChange(@NonNull AppActivity context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes, DeviceBean deviceBean, String title) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mDevice = deviceBean;
        mContext = context;
        setLeftText(title);
        String keynum = Objects.requireNonNull(LLDeviceUtil.getDeviceType(mDevice.getDeviceType())).getDeviceTypeInfo("keynum");
        String label = mContext.getString(mContext.getResources().getIdentifier("key_" + keynum,
                "string", mContext.getPackageName()));
        setRightText(label);
        setRightTextColor(AppApplication.getInstance().getThemeColor(R.attr.textColor2));
        setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        setRightTextSize(COMPLEX_UNIT_PX, DensityUtils.dip2px(context, 12));
        setOnClickListener(this::onClick);
    }

    @Override
    public void onClick(View v) {
        showDialog();
    }

    private BottomWheelViewDialog dialog;
    private void showDialog(){
        if (dialog == null){
            dialog = new BottomWheelViewDialog(mContext, getData());
            dialog.setLabel(getResources().getString(R.string.key_change));
            dialog.setDialogClickListener(new BottomWheelViewDialog.DialogClickListener() {
                @Override
                public void onConfirmClick(WheelValueBean deviceTypeBean) {
                    dialog.dismiss();
                    changeType(deviceTypeBean.getValue());
                }
            });
            new XPopup.Builder(getContext())
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(dialog);
        }
        dialog.setCurItem(mDevice.getDeviceType());
        dialog.show();
    }

    private List<WheelValueBean> getData(){
        String sType = Objects.requireNonNull(LLDeviceUtil.getDeviceType(mDevice.getDeviceType())).getDeviceTypeInfo("type");
        List<WheelValueBean> items = new ArrayList<>();
        for (DeviceType subType : DeviceType.values()){
            if (subType.getDeviceTypeInfo("type").equals(sType)){
                String keynum = subType.getDeviceTypeInfo("keynum");
                String label = mContext.getString(mContext.getResources().getIdentifier("key_" + keynum,
                        "string", mContext.getPackageName()));
                String value = subType.stringType;
                WheelValueBean bean = new WheelValueBean(label, value);
                items.add(bean);
            }
        }
        return items;
    }

    private void changeType(String deviceType){
        LLHttpManager.changeDeviceType(mContext, mDevice.getDeviceSerialNo(), deviceType, new HttpCallback<HttpData<Void>>(mContext){
            @Override
            public void onSucceed(HttpData<Void> result) {
                super.onSucceed(result);
                DeviceBaseConfigInfo deviceBaseConfigInfo = DeviceInfoParseUtil.parseData(getContext(), deviceType, DeviceBaseConfigInfo.class);
                mDevice.setDeviceType(deviceType);
                mDevice.setDeviceBaseConfigInfo(deviceBaseConfigInfo);
                String keynum = Objects.requireNonNull(LLDeviceUtil.getDeviceType(mDevice.getDeviceType())).getDeviceTypeInfo("keynum");
                String label = mContext.getString(mContext.getResources().getIdentifier("key_" + keynum,
                        "string", mContext.getPackageName()));
                setRightText(label);
                LLDeviceDBManager.getInstance(getContext()).put(mDevice);
                EventBus.getDefault().post(new DeviceItemChangeEvent(mDevice.getRoomSerialNo(), mDevice.getDeviceSerialNo(), mDevice.getIsFavourite()));
                EventBus.getDefault().post(new DeviceDetailChangeEvent(mDevice.getDeviceSerialNo()));
                mContext.toast(R.string.reset_success);
            }
        });
    }
}
